package com.hzy.fivepalace.admin.controller;

import com.alibaba.fastjson2.JSONObject;
import com.hzy.fivepalace.admin.annotation.Login;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.admin.dao.AdminAddGoods;
import com.hzy.fivepalace.admin.dao.AdminUpdGoods;
import com.hzy.fivepalace.admin.service.AdminGoodsService;
import com.hzy.fivepalace.common.util.ResponseUtil;
import com.hzy.fivepalace.common.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


/**
 * 商品服务
 *
 * @author gejian
 */
@RestController
@RequestMapping("/admin/goods")
@Api(tags = "商品服务")
@ResponseBody
@Login
public class AdminGoodsController extends BaseController {

    @Resource
    private AdminGoodsService goodsService;

    /**
     * 商品详情
     *
     * @param goodsId
     * @return
     */
    @SuppressWarnings("rawtypes")
    @PostMapping("/detail")
    @ApiOperation(value = "商品详情")
    public Object detail(@ApiParam(value = "商品ID") @RequestParam @NotNull Integer goodsId) {
        return goodsService.detail(goodsId);
    }

    /**
     * 商品列表
     *
     * @param categoryId
     * @param isOnSale
     * @param content
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/getGoodsList")
    @ApiOperation(value = "商品列表")
    public TableDataInfo getGoodsList(

            @ApiParam(value = "检索用:类目ID") @RequestParam(value = "categoryId", required = false) Integer categoryId,
            @ApiParam(value = "检索用:是否上架") @RequestParam(value = "isOnSale", required = false) Boolean isOnSale,
            @ApiParam(value = "检索用:内容") @RequestParam(value = "content", required = false) String content,
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "分页大小", defaultValue = "10") @RequestParam(defaultValue = "10") Integer size) {


        return goodsService.getGoodsList(page, size, categoryId, content, isOnSale);
    }

    /**
     * 商品上架下架
     *
     * @param goodsId
     * @param isOnSale
     * @return
     */
    @PostMapping("/updateOnSale")
    @ApiOperation(value = "商品上架下架")
    public Object updateOnSale(@ApiParam(value = "商品ID") @RequestParam @NotNull Integer goodsId,
                               @ApiParam(value = "是否上架") @RequestParam @NotNull Boolean isOnSale) {
        return goodsService.updateOnSale(goodsId, isOnSale);
    }

    /**
     * 删除商品
     *
     * @param goodsId
     * @return
     */
    @PostMapping("/delGoods")
    @ApiOperation(value = "删除商品")
    public Object delGoods(@ApiParam(value = "商品ID") @RequestParam @NotNull Integer goodsId) {
        return goodsService.delGoods(goodsId);
    }


    /**
     * 新增商品
     *
     * @param body
     * @return
     */
    @PostMapping("/addGoods")
    @ApiOperation(value = "新增商品")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object addGoods(@ApiParam(value = "json字符串") @RequestParam(value = "body") @NotBlank String body) {
        if (StringUtils.isBlank(body)) {
            return ResponseUtil.fail("参数为空");
        }
        try {
            AdminAddGoods dto = JSONObject.parseObject(body, AdminAddGoods.class);
            return goodsService.addGoods(dto);
        }catch (Exception  e){
            return ResponseUtil.fail("解析异常,参数错误");
        }
    }


    /**
     * 修改商品
     *
     * @param body
     * @return
     */
    @PostMapping("/updGoods")
    @ApiOperation(value = "修改商品")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object updGoods(@ApiParam(value = "json字符串") @RequestParam(value = "body") @NotBlank String body) {
        if (StringUtils.isBlank(body)) {
            return ResponseUtil.fail("参数为空");
        }
        try {
            AdminUpdGoods dto = JSONObject.parseObject(body, AdminUpdGoods.class);
            return goodsService.updGoods(dto);
        }catch (Exception  e){
            return ResponseUtil.fail("解析异常,参数错误");
        }
    }


    /**
     * 删除规格
     *
     * @param specificationId
     * @return
     */
    @PostMapping("/delSpecification")
    @ApiOperation(value = "删除规格")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public Object delSpecification(@ApiParam(value = "规格id") @RequestParam @NotNull Integer specificationId) {
        return goodsService.delSpecification(specificationId);
    }


}
